package com.springcloud.confing;

import com.springcloud.security.MyAccessDecisionManager;
import com.springcloud.security.MyInvocationSecurityMetadataSourceService;
import com.springcloud.security.MyUserDetailsService;
import com.springcloud.security.PasswordEncoderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

/**
 * @program: springcloud
 * @description: SpringSecurity的配置文件
 * @author: paddy
 * @create: 2019-04-15 12:47
 **/
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)  //  启用方法级别的权限认证
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private MyUserDetailsService myUserDetailsService;
    @Autowired
    private MyInvocationSecurityMetadataSourceService myInvocationSecurityMetadataSourceService;
    @Autowired
    private MyAccessDecisionManager myAccessDecisionManager;
//    @Autowired
//    private MySuccessHandler mySuccessHandler;
//    @Autowired
//    private MyFailureHandler myFailureHandler;
//    @Autowired
//    private MyLogoutSuccessHandler myLogoutHandler;
//    @Autowired
//    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
//    @Autowired
//    private MyFilterSecurityInterceptor myFilterSecurityInterceptor;


    //过滤器的配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
//                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)  // 禁用session
//                .and()
                .authorizeRequests()
                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O o) {
                        o.setSecurityMetadataSource(myInvocationSecurityMetadataSourceService);
                        o.setAccessDecisionManager(myAccessDecisionManager);
                        return o;
                    }
                })
                .antMatchers("/","/login","/index","/resource/tree","/user/currentUser","/actuator/**")// 允许直接访问的地址
                .permitAll()
                .anyRequest().authenticated()   // 其他地址的访问均需验证权限
                .and()
                .formLogin()
                .loginPage("/login")   //  登录页
//                .successHandler(mySuccessHandler)
                .successForwardUrl("/index")  // 成功后跳转页面
//                .failureHandler(myFailureHandler)
                .permitAll()
                .and()
                .logout()
//                .logoutSuccessHandler(myLogoutHandler)
                .logoutSuccessUrl("/index");

//        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
//        http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class);
//        http.addFilterAfter(jwtSuccessTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }


    //AuthenticatManager的配置
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder());
    }

//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        /*
//         * 在springboot中忽略静态文件路径，直接写静态文件的文件夹
//         * springboot默认有静态文件的放置路径，如果应用spring security，配置忽略路径
//         * 不应该从springboot默认的静态文件开始
//         * 如：在本项目中，所有的js和css都放在static下，如果配置忽略路径，则不能以static开始
//         * 配置成web.ignoring().antMatchers("/static/*");这样是不起作用的,如果忽略静态资源，页面样式可能无法加载
//         */
//
//        web.ignoring().antMatchers("/mystatic/**","/commons/**");
//    }

    @Bean
    public PasswordEncoder passwordEncoder() {

        return new PasswordEncoderImpl();
    }
}
